package com.dfc.springmvc.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dfc.springmvc.common.PageInfo;
import com.dfc.springmvc.dao.UserInfoDao;
import com.dfc.springmvc.dao.WeixinUserInfoDao;
import com.dfc.springmvc.pojo.UserInfo;
import com.dfc.springmvc.service.WexinUserService;
import com.dfc.springmvc.util.ParameterUtil;

/**
 * ���ݴ������ʵ��
 * 
 * @author dongdong
 * 
 */

@Service
public class WexinUserServiceImpl implements WexinUserService {

	Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private WeixinUserInfoDao userInfoDao;

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public UserInfo login(String userPhone, String password) {
		log.info("��֤�û�");
		return userInfoDao.login(userPhone, password);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public List<UserInfo> queryUsers(PageInfo<UserInfo> pageInfo, Map<String, String> map) {
		if (pageInfo != null) {
			log.info("���÷�ҳ");
			pageInfo.setTotalRecords(userInfoDao.getTotalRows(map));
		}
		log.info("��ȡ��¼");
		return userInfoDao.queryUsers(new RowBounds(pageInfo.getFromRecord(), pageInfo.getPageSize()), map);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public List<UserInfo> queryForJson() {
		log.info("��ȡ��¼");
		return userInfoDao.queryUsers();
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public UserInfo getUser(String userId) {
		return userInfoDao.getUser(userId);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void saveUser(UserInfo userInfo) {
		userInfoDao.saveUser(userInfo);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void updateUser(UserInfo userInfo) {
		userInfoDao.updateUser(userInfo);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void deleteUser(String[] idItem) {
		userInfoDao.deleteUser(idItem);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int checkUserPhone(String phone) {
		return userInfoDao.checkUserPhone(phone);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public List<UserInfo> getAllUsers() {
		return userInfoDao.getAllUsers();
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public UserInfo downloadFile(String userId) {
		return userInfoDao.downloadFile(userId);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int deleteFile(String userId) {
		return userInfoDao.deleteFile(userId);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int saveUserRole(String userId_role, String roleIds) {
		return ParameterUtil.SUCCESS;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void deleteUserRoleByUserId(String userId) {
		
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public String updatePWD(String userId, String oldpwd, String newpwd) {
		try {
			String password = userInfoDao.getUserPassword(userId);
			if (!oldpwd.equals(password)) {
				return "error";
			} else {
				userInfoDao.updatePWD(userId, newpwd);
				return "success";
			}
		} catch (Exception e) {
			return "fail";
		}
	}

}
